﻿using Distributed_ResourceMonitor.ResourceMonitor;
using Microsoft.AspNetCore.Mvc;
using ProductService.Services;
using ProductService.Services.Dtos;
using Servicecomb.Saga.Omega.Abstractions.Transaction;
using Volo.Abp.Application.Dtos;
using Volo.Abp.AspNetCore.Mvc;

namespace ProductService.Controllers;

[Route("api/productservice/[action]")]
public class ProductController : AbpController, IProductService
{
    //public ResourcesMoniters resourcesMoniters { get; set; }  //资源监控

    /// <summary>
    /// 1、先创建Counter
    /// </summary>
    //private static readonly Counter _requestCounter = Metrics.CreateCounter("productservice", "productservice_help", new CounterConfiguration  //资源监控
    //{
    //    LabelNames = new[] { "method", "endpoint" }
    //});

    public IProductService ProductService { get; set; }

    public ProductController()
    {

    }

    /// <summary>
    /// 资源监控
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    //[ResourcesMoniters]
    //[Monitor("productservice", "DeleteAsync")]
    [HttpPost]
    public async Task<ProductDto> CreateAsync(ProductDto input)
    {
        //resourcesMoniters.ResourcesCounter("Post", "Order/reate");
        var dto = await ProductService.CreateAsync(input);
        //throw new Exception("添加结果失败");
        return dto;
    }

    /// <summary>
    /// 资源监控
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    //[ResourcesMoniters]
    //[Monitor("productservice", "DeleteAsync")]
    //[Compensable(nameof(DeleteProduct))]
    [HttpDelete]
    public async Task DeleteAsync(Guid id)
    {
        await ProductService.DeleteAsync(id);
    }

    [HttpGet]
    //[Monitor("productservice", "GetAsync")]
    public async Task<ProductDto> GetAsync(Guid id)
    {
        return await ProductService.GetAsync(id);
    }

    [HttpPost]
    //[Monitor("productservice", "GetListAsync")]
    public async Task<PagedResultDto<ProductDto>> GetListAsync(PagedAndSortedResultRequestDto input)
    {
        //throw new Exception("未查询到相关信息");
        return await ProductService.GetListAsync(input);
    }

    /// <summary>
    /// 回滚方法---> 相反操作！--->补偿操作
    /// </summary>
    /// <param name="createProductDto"></param>
    void DeleteProduct(ProductDto createProductDto)
    {
        //1、删除商品

    }

    [HttpPut]
    public async Task<ProductDto> UpdateAsync(Guid id, ProductDto input)
    {
        return await ProductService.UpdateAsync(id, input);
    }
}
